<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div @click="Gowelcome">
        <img src="@/assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <el-tooltip class="item" effect="dark" content="展开与折叠菜单" placement="right">
          <div class="toggle-btn" @click="toggleCollapse">|||</div>
        </el-tooltip>
        <!-- 侧边栏菜单 -->
        <!-- unique-opened ===  :unique-opened=true -->
        <el-menu
          unique-opened
          :collapse="isCollapse"
          background-color="#333744"
          text-color="#fff"
          router
          :default-active="activePath"
          :collapse-transition=false
          active-text-color="#409fff">
          <!-- 一级菜单 -->
          <!-- 这里的index只接收字符串,不接受数值 ==>> :index="item.id+''"(数值和字符串拼接会得到字符串)-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
           <el-menu-item
           :index="'/'+subItem.path + ''"
           v-for="subItem in item.children"
           @click="saveNavState('/'+subItem.path + '')"
           :key="subItem.id">
              <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </template>
           </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧主体 -->
      <el-main>
        <!-- 路由的占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
</el-container>
</template>
<script>
export default {
  data(){
    return{
      // 左侧菜单数据
      menuList:[],
      iconsObj:{
        '125':'el-icon-s-check',
        '103':'el-icon-box',
        '101':'el-icon-shopping-bag-1',
        '102':'el-icon-notebook-2',
        '145':'el-icon-data-line'
      },
      // 折叠按钮
      isCollapse:false,
      // 保存激活的连接(被激活的连接地址)
      activePath:""
    }
  },
  created(){
    this.getMenuList()
    this.activePath=window.sessionStorage.getItem("activePath")
  },
  methods:{
    // 退出功能
    logout(){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           window.sessionStorage.clear();
          this.$router.push('/login')
          this.$message.success('退出成功!');
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    // 获取所有的左侧菜单
    getMenuList(){
      this.axios('menus').then(res=>{
        console.log(res)
        if(res.data.meta.status !==200){
          return this.$message.error(res.data.data.meta.msg)
        }
        this.menuList=res.data.data
      })
    },
    // 菜单的折叠与展开
    toggleCollapse(){
      this.isCollapse= !this.isCollapse
    },
    // 保存左侧菜单连接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath=activePath
    },
    // 返回welcome组件
    Gowelcome(){
      this.$router.push("/welcome")
      this.activePath=''
    },
  }
}
</script>
<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color:#373d41;
  display:flex;
  justify-content:space-between;
  align-items:center;
  color: #fff;
  font-size: 20px;
  >div{
    display:flex;
    align-items:center;
    img{
      width: 50px;
      height: 50px;
    }
    span{
      margin-left: 15px;
    }
  }
}
.el-container{
  .el-aside{
    background-color:#333744;
    .el-menu{
      border-right: 0;
      .el-submenu {
      i{
        margin-right: 10px;
      }
    }
    }
  }
  .el-main{
    background-color:#eaedf1
  }
}
.toggle-btn{
  background-color:#4a5064;
  font-size: 10px;
  line-height:24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
